<?php
/*
自定义引入类需要在composer.json 中定义和引入 然后再更新 composer dump-autoload
*/
namespace extend\services\warehouse;
use Illuminate\Support\Facades\DB;//使用数据操作
use Illuminate\Support\Facades\Cache;
use extend\services\nailin\NewdemandService;//使用金蝶接口
use Modules\User\Models\User;
use SimpleSoftwareIO\QrCode\Facades\QrCode;   
use Modules\Warehouse\Models\Stash;


class warehouseService
{
    
    
    /**
    * 根据金蝶仓库数据
    * @param $sku
    */
   
    public function warehousedata($request){
        
        if(empty($request)){
             $warehousedata = Cache::has('warehousedata');      
            if(!empty($warehousedata)){
                return Cache::get('warehousedata');
                exit;
            } 
        }
     

        $kingDeeService = new NewdemandService();
        //字段
        $fields   =  ['FSTOCKID','FNumber','FName','FUseOrgId.FName'];
        //条件
        $where = '';
        if(!empty($request)){
           
            if(isset($request['FName'])){
                $where = "FName = '{$request['FName']}'";
            }else if(isset($request['FNumber'])){
                $where = "FNumber = '{$request['FNumber']}'";
            }else{
                $where = "FName='{$request['FName']}' AND FNumber='{$request['FNumber']}' AND";
            }
        }else{
            $where  = "FDOCUMENTSTATUS='C' AND FFORBIDSTATUS='A'";
        }
       
        $postJson   = [
            'FormId'        => 'BD_STOCK',
            'FieldKeys'     => implode(',',$fields),
            'FilterString'  => $where,
            //'FilterString'  => [],
            'OrderString'   => '',
            'TopRowCount'   => 0,
            'StartRow'      => 0,
            'Limit'         => 0,
            'SubSystemId'   => '',
        ];
        
        $postJson = $kingDeeService->create_postdata([json_encode($postJson)]);
        $reslut = $kingDeeService->ExecuteBillQuery($kingDeeService->cloudUrl, $postJson);
      
        if(empty($reslut)) throw new \Exception('接口返回为空');
        $reslut = json_decode($reslut,true);
        if(!isset($reslut[0]) || empty($reslut[0])) return [];
       
        if(!empty($request)){
            return self::listToValue($reslut,$fields);
            exit;
        }else{
            Cache::put('warehousedata',self::listToValue($reslut,$fields),7200);
            return self::listToValue($reslut,$fields);
            exit;
        }
       
    }

    public function warehousedataTwo($request){
        
        if(!empty($request)){
            
            if(!isset($request['owning_id'])){
                if(Cache::has('warehousedata')) return Cache::get('warehousedata');exit;
            }
        }
        
        $kingDeeService = new NewdemandService();
        //字段
        $fields   =  ['FSTOCKID','FNumber','FName','FUseOrgId.FName'];
        //条件
        $where = '';
        if(!empty($request)){
            if(isset($request['owning_id'])){
                $where = "FName = '{$request['owning_id']}'";
            }
            
        }else{
            $where  = "FDOCUMENTSTATUS='C' AND FFORBIDSTATUS='A'";
        }
       
        $postJson   = [
            'FormId'        => 'BD_STOCK',
            'FieldKeys'     => implode(',',$fields),
            'FilterString'  => $where,
            //'FilterString'  => [],
            'OrderString'   => '',
            'TopRowCount'   => 0,
            'StartRow'      => 0,
            'Limit'         => 0,
            'SubSystemId'   => '',
        ];
        
        $postJson = $kingDeeService->create_postdata([json_encode($postJson)]);
        $reslut = $kingDeeService->ExecuteBillQuery($kingDeeService->cloudUrl, $postJson);
      
        if(empty($reslut)) throw new \Exception('接口返回为空');
        $reslut = json_decode($reslut,true);
        if(!isset($reslut[0]) || empty($reslut[0])) return [];
       
        if(!empty($request)){
            return self::listToValue($reslut,$fields);
            exit;
        }else{
            Cache::put('warehousedata',self::listToValue($reslut,$fields),7200);
            return self::listToValue($reslut,$fields);
            exit;
        }
       
    }


    

    /**
     * 列表数据转键值对
     * @param $list
     * @param $fields
     * @return array
     */
    public function listToValue($list,$fields){
        $newList = [];
        foreach ($list as $key => $val){
            foreach ($val as $itemKey => $itemVal){
                $newList[$key][$fields[$itemKey]] = $itemVal;
            }
        }
        return $newList;
        
    }


    /**
     * 用户列表接口
     * @param $list
     * @param $fields
     * @return array
     */
    public function uerdata(){
        $User = User::get();//用户
        $datauser  = [];
        foreach($User as $k=>$v){
            $datauser[$v['id']]['username'] = $v['username'];
            $datauser[$v['id']]['phone'] = $v['phone'];
            $datauser[$v['id']]['id'] = $v['id'];
        }
       
        return $datauser;
        
    }


    /**
     * 所属仓库接口
     * @return mixed
     */
    public function warehousedataApi()
    {
        $resdata = [];
        $data = self::warehousedata($resdata);
        $resdata = [];
        foreach($data as $k=>$v){
            $resdata[$k]['label'] = $v['FName'];
            $resdata[$k]['value'] = $v['FSTOCKID'];
        }
     

        return $resdata;
    }

      /**
     * 所属仓库接口
     * @return mixed
     */
    public function warehousedataApiTwo()
    {
        $Stash = Cache::has('Stash');      
        if(!empty($Stash)){
            return Cache::get('Stash');
            exit;
        } 

        $data= Stash::select('id','fmaterial','fnumber','fname')->get()->toArray();
        $resdata = [];
        foreach($data as $k=>$v){
            $resdata[$v['id']]['FName'] = $v['fname'];
            $resdata[$v['id']]['FNumber'] = $v['fnumber'];
        }
        Cache::put('Stash',$resdata,7200);
        return $resdata;
    }

       /**
     * 所属仓库接口
     * @return mixed
     */
    // public function warehousedataApiTwo()
    // {
    //     $resdata = [];
    //     $data = self::warehousedata($resdata);
    //     $resdata = [];
    //     foreach($data as $k=>$v){
    //         $resdata[$v['FSTOCKID']]['FName'] = $v['FName'];
    //         $resdata[$v['FSTOCKID']]['FNumber'] = $v['FNumber'];
    //     }
    //     return $resdata;
    // }



     /**
    * @param int $size  此参数为二维码的尺寸，是像素值
    * @param string $info 此参数为二维码的内容，内容为文字时，扫描展示的是此文字；
    *       内容为http://或https://开头的网址时，打开的就是此网址
    * @return mixed
    * $pngUrl 二维码名称
    */

   public function createQrCode($size,$info,$pngUrl)
   {
       $size = $size ?: 400;
       $info = $info ?: "success";
       $res = QrCode::encoding ('UTF-8')->format('png')->size($size)->color(000,000,000)->generate($info,public_path('uploads/barcode/'.$pngUrl.'.png'));
        //$res = QrCode::size($size)->color(000,000,000)->generate($info);
       return $res;

   }


    
    



}